<template>
  <div class="app-container">
    <el-button type="primary" @click="dialogFormVisible = true" size="mini">导入配货商品</el-button>

    <el-dialog title="导入配货商品" icon="el-icon-search" :visible.sync="dialogFormVisible"  style="width:880px">
      <el-form :model="form_import">
        <el-form-item label="活动区域" :label-width="formLabelWidth">
          <el-select v-model="form_import.djlx" placeholder="请选择导入单据">
            <el-option label="商店配货通知单" value="psend"></el-option>
            <el-option label="商品进货单" value="jsend"></el-option>
            <el-option label="从Excel导入" value="excel"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单据编号" :label-width="formLabelWidth">
          <el-input v-model="form_import.djbh" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary"  @click="handleImport" >确 定</el-button>
      </div>
    </el-dialog>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="78px">
<!--      <el-form-item label="日期" prop="name">-->
<!--        <el-date-picker-->
<!--          v-model="daterange"-->
<!--          type="daterange"-->
<!--          :picker-options="pickerOptions"-->
<!--          range-separator="至"-->
<!--          start-placeholder="开始日期"-->
<!--          end-placeholder="结束日期"-->
<!--          align="right"-->
<!--          size="mini">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->

      <el-form-item label="筛选商品" prop="name">
        <el-select v-model="shangpinval"
                   multiple
                   collapse-tags
                   placeholder="请选择">
          <el-option-group
            v-for="group in options"
            :key="group.label"
            :label="group.label">
            <el-option
              v-for="item in group.options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-option-group>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:psendinfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:psendinfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:psendinfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:psendinfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="psendinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="cpuId" />
      <el-table-column label="CPU名称" align="center" prop="name" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:psendinfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:psendinfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改CPU型号对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="CPU名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入CPU名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
console.log("test");
import { listShangpinByPsend, listPsendinfo,getPsendinfo, delPsendinfo, addPsendinfo, updatePsendinfo } from "@/api/system/distribution";

export default {
  name: "Distribution",
  data() {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一年',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 365);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近二年',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 730);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三年',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 1095);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      daterange: '', //日期选择
      options: [{
        label: '年份',
        options: [{
          value: 'YEAR:2025',
          label: '2025年'
        }, {
          value: 'YEAR:2024',
          label: '2024年'
        }]
      }, {
        label: '大类',
        options: [{
          value: 'DALEI:BQ',
          label: '半裙'
        }, {
          value: 'DALEI:CK',
          label: '长裤'
        }, {
          value: 'DALEI:CQ',
          label: '长裙'
        }, {
          value: 'DALEI:DD',
          label: '吊带'
        }, {
          value: 'DALEI:DY',
          label: '大衣'
        }, {
          value: 'DALEI:MJ',
          label: '马甲'
        }]
      }, {
        label: '季节',
        options: [{
          value: 'JIJIE:A',
          label: '春'
        }, {
          value: 'JIJIE:B',
          label: '夏'
        }, {
          value: 'JIJIE:C',
          label: '秋'
        }, {
          value: 'JIJIE:D',
          label: '冬'
        }]
      }, {
        label: '品牌',
        options: [{
          value: 'PINPAI:ADM',
          label: '爱迪曼'
        }, {
          value: 'PINPAI:B6',
          label: '蓝贝紫'
        }, {
          value: 'PINPAI:KSL',
          label: '珂赛琳'
        }, {
          value: 'PINPAI:LBZ',
          label: '蓝贝紫'
        }]
      }, {
        label: '商品类别（商品属性1）：',
        options: [{
          value: 'FJSX1:001',
          label: 'A组'
        }, {
          value: 'FJSX1:002',
          label: 'B组'
        }, {
          value: 'FJSX1:003',
          label: '杨尚组'
        }, {
          value: 'FJSX1:004',
          label: '定价组'
        }]
      }, {
        label: '材质(商品属性2)',
        options: [{
          value: 'FJSX2:000',
          label: '未定义'
        }]
      }, {
        label: '供应商(商品属性4)',
        options: [{
          value: 'FJSX4:001',
          label: '调帐'
        }, {
          value: 'FJSX4:002',
          label: '杨尚'
        }, {
          value: 'FJSX4:01',
          label: '艾思德妮'
        }, {
          value: 'FJSX4:02',
          label: '柏蔻'
        }]
      }, {
        label: '面料成分(商品属性7)',
        options: [{
          value: 'FJSX7:007',
          label: '莱赛尔纤维70% 聚酯纤维30%'
        }, {
          value: 'FJSX7:033',
          label: '新疆棉33% 详见内标'
        }, {
          value: 'FJSX7:035',
          label: '棉35% 详见内标'
        }, {
          value: 'FJSX7:068',
          label: '聚酯纤维68% 粘胶纤维20% 天丝5%氨纶4%羊毛3%'
        }]
      }, {
        label: '面料成分2(商品属性8)',
        options: [{
          value: 'FJSX8:01',
          label: '49%锦纶  11%氨纶'
        }, {
          value: 'M65',
          label: '棉65% 详见内标'
        }]
      }, {
        label: '商品风格(商品属性9)',
        options: [{
          value: 'FJSX9:001',
          label: '时尚款'
        }, {
          value: 'FJSX9:002',
          label: '休闲款'
        }, {
          value: 'FJSX9:003',
          label: '成熟款'
        }, {
          value: 'FJSX9:004',
          label: '形象款'
        }, {
          value: 'FJSX9:005',
          label: '西装款'
        }, {
          value: 'FJSX9:006',
          label: '牛仔款'
        }, {
          value: 'FJSX9:007',
          label: '年轻款'
        }, {
          value: 'FJSX9:008',
          label: '国风系'
        }, {
          value: 'FJSX9:009',
          label: '小香风款'
        }]
      }, {
        label: '商品色系(商品属性10)',
        options: [{
          value: 'FJSX10:001',
          label: '柔色系'
        }, {
          value: 'FJSX10:002',
          label: '亮色系'
        }, {
          value: 'FJSX10:003',
          label: '深色系'
        }, {
          value: 'FJSX10:004',
          label: '花色系'
        }]
      }, {
        label: '商品外形(商品属性11)',
        options: [{
          value: 'FJSX11:001',
          label: '短款'
        }, {
          value: 'FJSX11:002',
          label: '一手长款'
        }, {
          value: 'FJSX11:003',
          label: '中长款'
        }, {
          value: 'FJSX11:004',
          label: '长款'
        }]
      }],
      shangpinval: '',  //商品筛选


      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // CPU型号表格数据
      psendinfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null
      },
      // 表单参数
      form: {},
      dialogFormVisible: false,
      form_import: {
        djbh: '',
        djlx: '',
      },
      formLabelWidth: '120px',
      // 表单校验
      rules: {
        name: [
          { required: true, message: "CPU名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 从商店配货通知单导入商品 */
    getShangPinListByPsend() {
      this.loading = true;
      listPsendinfo(this.queryParams).then(response => {
        this.psendinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询CPU型号列表 */
    getList() {
      this.loading = true;
      listPsendinfo(this.queryParams).then(response => {
        this.psendinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cpuId: null,
        name: null
      };
      this.resetForm("form");
    },
    /** 导入确定按钮操作 */
    handleImport() {
      console.log(this.form_import)
      const djbh=this.form_import.djbh;
      if(this.form_import.djlx == 'psend'){
          getShangPinListByPsend();
      }
      this.dialogFormVisible = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cpuId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加CPU型号";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const cpuId = row.cpuId || this.ids
      getPsendinfo(cpuId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改CPU型号";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.cpuId != null) {
            updatePsendinfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPsendinfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cpuIds = row.cpuId || this.ids;
      this.$modal.confirm('是否确认删除CPU型号编号为"' + cpuIds + '"的数据项？').then(function() {
        return delPsendinfo(cpuIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/psendinfo/export', {
        ...this.queryParams
      }, `psendinfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>



